#include "qkeyboardhost.h"

#include "../qkeyboardwidget.h"

#include "../property/qabstractproperty.h"
#include "../qpropertyfactory.h"

#include <QWidget>
#include <QPushButton>

QKeyBoardHost::QKeyBoardHost(QAbstractHost *parent):
    QAbstractWidgetHost(parent)
{
    setProperty("title",KEYBOARD_TITLE);
}

void QKeyBoardHost::create_object()
{
    m_object=new QKeyBoardWidget;
    ((QWidget*)m_object)->setVisible(false);
}

void QKeyBoardHost::init_property()
{
    QAbstractWidgetHost::init_property();

    remove_property("level");
    remove_property("enabled");
    remove_property("cursor");
    remove_property("focusPolicy");
    remove_property("toolTip");
    remove_property("mousePress");
    remove_property("mouseMove");
    remove_property("mouseRelease");
    remove_property("doubleClick");

    QAbstractProperty *pro=this->get_property("geometry");
    if(pro!=NULL)
    {
        pro->set_attribute("show_name",tr("Size"));
        pro->set_attribute(QString("x:")+ATTR_VISIBLE,false);
        pro->set_attribute(QString("y:")+ATTR_VISIBLE,false);
    }

    pro=QPropertyFactory::create_property("Point");
    if(pro!=NULL)
    {
        pro->set_property("name","position");
        pro->set_attribute("show_name",tr("Position"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        m_object->setProperty("position",QPoint(0,300));
        insert_property(pro);
    }

    pro=this->get_property("objectName");
    if(pro!=NULL)
    {
        pro->set_attribute("editabled",false);
    }

    set_property_value("geometry",QRect(0,0,800,300));
}

bool QKeyBoardHost::handle_mouse_release_event(QMouseEvent *event)
{
    return true;
}

bool QKeyBoardHost::handle_mouse_press_event(QMouseEvent *event)
{
    return true;
}

bool QKeyBoardHost::handle_mouse_move_event(QMouseEvent *event)
{
    return true;
}

bool QKeyBoardHost::handle_double_clicked_event(QMouseEvent *event)
{
    return true;
}

bool QKeyBoardHost::handle_context_menu_event(QContextMenuEvent *event)
{
    return true;
}

void QKeyBoardHost::connect_button()
{
    QList<QAbstractHost*> list=m_children;
    while(list.size()>0)
    {
        QAbstractHost *host=list.takeFirst();
        if(host->inherits("QKeyboardButtonHost"))
        {
            QPushButton *button=(QPushButton*)host->get_object();
            disconnect(button,SIGNAL(clicked()),this,SLOT(key_button_clicked()));
            connect(button,SIGNAL(clicked()),this,SLOT(key_button_clicked()));
        }
        list+=host->get_children();
    }
}

void QKeyBoardHost::key_button_clicked()
{
    int key=sender()->property("key").toInt();
    if(key!=0)
    {
        ((QKeyBoardWidget*)m_object)->send_key(key);
    }
}
